<template>
  <div class="app-container">
    <!-- 条件查询 -->
    <search
      :query="query"
      @queryData="queryData"
      @add="handleAdd(0)"
      @reload="reload"
      :labelArr="labelArr"
      :selectFalse="selectFalse"
    />
    <!-- 数据列表 :data 绑定渲染的数据, border 纵向边框 -->
    <!-- 渲染树形数据时，必须要指定 row-key, list集合中的 id属性 -->
    <el-table
      :data="list"
      border
      highlight-current-row
      row-key="id"
      style="width: 100%"
    >
      <!-- type="index"获取索引值，从1开始 ，label显示标题，prop 数据字段名，width列宽 -->
      <el-table-column
        align="center"
        type="index"
        label="序号"
        width="60"
      ></el-table-column>
      <el-table-column align="center" prop="name" label="名称">
      </el-table-column>
      <el-table-column align="center" prop="url" label="请求地址">
      </el-table-column>
      <el-table-column prop="code" label="权限标识"></el-table-column>
      <el-table-column align="center" prop="status" label="类型">
        <template slot-scope="scope">
          <span v-if="scope.row.type === 1">目录</span>
          <span v-if="scope.row.type === 2">菜单</span>
          <span v-else>按钮</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="icon" label="图标">
        <template slot-scope="scope">
          <i :class="scope.row.icon" />
        </template>
      </el-table-column>
      <el-table-column align="center" prop="sort" label="排序">
      </el-table-column>
      <el-table-column align="center" label="操作" width="260">
        <template slot-scope="scope">
          <el-button type="success" size="mini" @click="handleAdd(scope.row.id)"
            >新增</el-button
          >
          <el-button
            type="primary"
            size="mini"
            @click="handleEdit(scope.row.id)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)"
            >删除
          </el-button>
        </template></el-table-column
      >
    </el-table>
    <!-- 弹窗 -->
    <edit
      :dialogVisible="edit.dialogVisible"
      :title="edit.title"
      :formData="edit.formData"
      :remoteForm="remoteForm"
    />
  </div>
</template>

<script>
import { ApiGetMenuList, ApiGetById, ApiDeleteById } from '@/api/menu'
import search from '@/components/common/header-search.vue'
import edit from './components/edit.vue'
export default {
  name: 'Menu',
  components: {
    search,
    edit
  },
  data () {
    return {
      query: {}, // 查询条件
      list: [], // 列表数据
      selectFalse: false, // 控制按钮显示隐藏
      labelArr: ['菜单标题'],
      edit: {
        dialogVisible: false,
        title: '新增',
        formData: {}
      }
    }
  },
  methods: {
    async fetchData () { // 查询菜单列表
      const { data } = await ApiGetMenuList(this.query)
      this.list = data
    },
    handleAdd (id) { // 新增操作
      // 如果新增id=0是头部的新增 否则是表格内的
      this.edit.formData.parentId = id
      this.edit.title = '新增'
      this.edit.dialogVisible = true
    },
    async handleEdit (id) { // 编辑操作
      const res = await ApiGetById(id)
      if (res.code === 20000) {
        this.edit.formData = res.data
        this.edit.title = '编辑'
        this.edit.dialogVisible = true
      }
    },
    handleDelete (id) { // 删除操作
      this.$confirm('确认删除这条记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await ApiDeleteById(id)
        this.$message({
          type: res.code === 20000 ? 'success' : 'warn',
          message: res.message
        });
        // 刷新列表
        this.fetchData()
      }).catch(() => {

      });
    },
    queryData () { // 文章查询
      this.fetchData()
    },
    reload () { // 重置
      this.query = {}
      this.fetchData()
    },
    remoteForm () { // 关闭弹窗
      this.edit.formData = {}
      this.edit.dialogVisible = false
      this.fetchData()
    },
  },
  created () {
    this.fetchData()
  },
}
</script>

<style>
</style>